import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_rating_bar/flutter_rating_bar.dart';

/// 2022年更新  适配 flutter 3.0
class TextFieldPage02 extends StatefulWidget {
  const TextFieldPage02({super.key});

  @override
  State<StatefulWidget> createState() {
    return TextFieldPage02State();
  }
}

class TextFieldPage02State extends State {
  late TextEditingController controller = TextEditingController();

  @override
  void dispose() {
    controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Padding(
          child: buildCenter(),
          padding: const EdgeInsets.all(20),
        ),
      ),
    );
  }

  buildCenter() {
    return Column(
      mainAxisSize: MainAxisSize.min,
      children: [
        TextField(controller: controller),
        Row(
          children: <Widget>[
            TextButton(
              onPressed: () {
                final String selectedString = controller.text.substring(
                  controller.selection.baseOffset,
                  controller.selection.extentOffset,
                );
                Clipboard.setData(ClipboardData(text: selectedString));
              },
              child: const Text("复制"),
            ),
            TextButton(
              child: const Text("选中全部"),
              onPressed: () {
                controller.selection = controller.selection.copyWith(
                  baseOffset: 0,
                  extentOffset: controller.text.length,
                  affinity: controller.selection.affinity,
                );
              },
            ),
            TextButton(
              onPressed: () {
                controller.clear();
              },
              child: const Text("清空"),
            ),
            const Spacer(),
          ],
        )
      ],
    );
  }
}
